package org.csu.mypetstore.controller;


import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.UserInfo;
import org.csu.mypetstore.service.AccountService;
import org.csu.mypetstore.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.util.DigestUtils;//MD5加密
import org.springframework.web.bind.support.SessionStatus;

import javax.servlet.http.HttpSession;
import java.util.List;


@Controller
@RequestMapping("/account")
@SessionAttributes(value = {"account", "order", "cart", "code","userInfo","userInfoList"})
public class AccountController {

      @Autowired
      private AccountService accountService;

      @Autowired
      private UserInfoService userInfoService;


      //前往登录页面
      @GetMapping("/signonForm")
      public String SignOnForm() {

            return "account/SignonForm";
//            return "account/SignonByCode";
//            return  "manager/list";

      }

      //前往验证码登录
      @GetMapping("/signonByCode")
      public String SignOnByCode() {

            return "account/SignonByCode";
      }

      //登录
      @PostMapping("/signon")

      public String SignOn(String root, String username, String password, Model model) {

            password = DigestUtils.md5DigestAsHex(password.getBytes());//加密

            Account account = accountService.getAccount(username, password);

            if (account == null) {
                  String message = "用户名或密码错误登录失败";
                  model.addAttribute("message", message);
//                  setMessage(value);
//                  clear();
//                  return new ForwardResolution(SIGNON);
                  return "account/SignonForm";
            } else {
                  UserInfo userInfo = userInfoService.getUserInfo(account.getUsername());
                  model.addAttribute("account", account);
                  model.addAttribute("userInfo",userInfo);
//                测试
                  System.out.println("权限为"+userInfo.getRoot());
                  List<UserInfo> userInfoList = userInfoService.getUserInfoList();
                  model.addAttribute("userInfoList",userInfoList);
                  return "catalog/main";
            }
      }

      @PostMapping("/signonByCode")
      public String SignOnByCode(String memPhone, String code, HttpSession httpSession, Model model) {

//            String code1 = (String) getAttribute("code");

            String code1 = (String) httpSession.getAttribute("code");
            if (code1.equals(code)) {

                  Account account = accountService.getAccountByPhone(memPhone);
                  if (account == null) {
                        String message = "该手机号账户不存在";
                        model.addAttribute("message", message);

                        return "common/Error";
                  } else {
                        model.addAttribute("account", account);
                  }
            }
            System.out.println("登陆成功");
            return "catalog/main";
      }


      @GetMapping("/signOut")
      public String SignOff(Model model, HttpSession session) {

            session.removeAttribute("account");
            model.addAttribute("account", null);
            return "catalog/main";
      }


      @GetMapping("/goToRegister")
      public String GotoRegister(Model model) {
            return "account/NewAccountForm";
      }

      @PostMapping("/register")
      public String Register(String username, String password, Model model) {

            if (username != null && password != null) {

                  password = DigestUtils.md5DigestAsHex(password.getBytes());//加密


                  Account account = accountService.getAccount(username);
                  if (accountService.getAccount(username) == null) {
                        Account account1 = new Account(username, password);

                        accountService.insertAccount(account1);

                        model.addAttribute("account", account1);
                        model.addAttribute("message", "注册成功");
                  } else {
                        model.addAttribute("message", "用户名已存在");
                  }
            } else
                  model.addAttribute("message", "请输入用户名和密码");

            return "account/NewAccountForm";
      }


      @GetMapping("/goToMyAccount")
      public String GotoMyAccount(Model model) {
            return "account/EditAccountForm";
      }


      @PostMapping("/saveAccountInfo")
      public String SaveAccountInfo(String password, Model model) {

            password = DigestUtils.md5DigestAsHex(password.getBytes());//加密

            Account account = (Account) model.getAttribute("account");
            account.setPassword(password);

            accountService.updateAccount(account);

            return "account/EditAccountForm";
      }

      @RequestMapping("/userManager")
      public String userManager(Model model){
            return "managerUser/UserManage";
      }

      @RequestMapping("/userEdit")
      public String userEdit(String username,Model model){
            UserInfo userInfo = userInfoService.getUserInfo(username);
            model.addAttribute("userInfo",userInfo);
            return "managerUser/userEdit";
      }

      @RequestMapping("/userEditPassword")
      public String userEditPassword(String username,Model model){
            UserInfo userInfo = userInfoService.getUserInfo(username);
            model.addAttribute("userInfo",userInfo);
            return "managerUser/userEditPassword";
      }
      @RequestMapping("/userUpdatePassword")
      public String userUpdate(String username,
                               String password,Model model){

            UserInfo userInfo = userInfoService.getUserInfo(username);

            password = DigestUtils.md5DigestAsHex(password.getBytes());//加密

            userInfo.setPassword(password);
            userInfoService.updateUserInfo(userInfo);

            List<UserInfo> userInfoList = userInfoService.getUserInfoList();
            model.addAttribute("userInfoList",userInfoList);
            return "managerUser/UserManage";
      }



      @RequestMapping("/userUpdate")
      public String userUpdate(String username,
                               String password,
                               String email,
                               String firstName,
                               String lastName,
                               String status,
                               String address1,
                               String address2,
                               String city,
                               String state,
                               String zip,
                               String country,
                               String phone,
                               Model model
                               ){
            UserInfo userInfo = new UserInfo();
            userInfo.setUsername(username);
            userInfo.setPassword(password);
            userInfo.setEmail(email);
            userInfo.setFirstName(firstName);
            userInfo.setLastName(lastName);
            userInfo.setStatus(status);
            userInfo.setAddress1(address1);
            userInfo.setAddress2(address2);
            userInfo.setCity(city);
            userInfo.setState(state);
            userInfo.setZip(zip);
            userInfo.setCountry(country);
            userInfo.setPhone(phone);
            userInfoService.updateUserInfo(userInfo);

            System.out.println("进入函数");
            System.out.println("hsj" + country);
            List<UserInfo> userInfoList = userInfoService.getUserInfoList();
            model.addAttribute("userInfoList",userInfoList);
            return "managerUser/UserManage";
      }

}
